package org.quickstart.server.guice;

import com.allen_sauer.gwt.log.client.RemoteLoggerService;
import com.allen_sauer.gwt.log.server.RemoteLoggerServiceImpl;
import com.google.inject.Singleton;
import com.google.inject.servlet.ServletModule;
import net.customware.gwt.dispatch.server.service.DispatchServiceServlet;
import org.quickstart.server.GuiceRemoteServiceServlet;

import static java.lang.String.format;

/**
 * @author Yoav Aharoni
 */
public class DefaultGwtServerModule extends ServletModule {
	private String moduleName;

	public DefaultGwtServerModule(String moduleName) {
		this.moduleName = moduleName;
	}

	@Override
	public void configureServlets() {
		// GWT Dispatcher
		bind(DispatchServiceServlet.class).in(Singleton.class);
		serve(format("/%s/dispatch", moduleName)).with(DispatchServiceServlet.class);

		// Guice RPC
		bind(GuiceRemoteServiceServlet.class).in(Singleton.class);
		serve(format("/%s/gwt-rpc", moduleName)).with(GuiceRemoteServiceServlet.class);

		// GWT-log
		bind(RemoteLoggerService.class).to(RemoteLoggerServiceImpl.class).in(Singleton.class);
		bind(RemoteLoggerServiceImpl.class).in(Singleton.class);
		serve(format("/%s/gwt-log", moduleName)).with(GuiceRemoteServiceServlet.class);
	}
}
